package cn.elead.it.sso.core.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.elead.it.sso.core.constant.GlobalVariable;
import cn.elead.it.sso.core.model.SsoUser;
import cn.elead.it.sso.core.store.SsoLoginStore;
import cn.elead.it.sso.core.store.SsoSessionIdHelper;
import cn.elead.it.sso.core.util.CookieUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * App单点登录处理工具(token)
 * </p>
 *
 * @author luopeng
 */
@Slf4j
public class SsoTokenLoginHelper {

	/**
	 * 客户端登录
	 * 
	 * @param sessionId
	 * @param ssoUser
	 */
	public static void login(String sessionId, SsoUser ssoUser) {
		log.info("login sessionId:{}", sessionId);
		String storeKey = SsoSessionIdHelper.parseStoreKey(sessionId);
		if (storeKey == null) {
			throw new RuntimeException("parseStoreKey Fail, sessionId:" + sessionId);
		}
		String result = SsoLoginStore.put(storeKey, ssoUser);
		if (!StrUtil.equals(result, "OK")) {
			throw new RuntimeException("login Fail, sessionId:" + sessionId);
		}
		SsoUser user = SsoLoginStore.get(storeKey);
		if(ObjectUtil.isNull(user)) {
			throw new RuntimeException("login Fail, user not exist:" + sessionId);
		}
		log.info("storeKey:{}  sessionId:{} login success", storeKey, sessionId);
		log.info("user:{} login success", user);
	}

	/**
	 * 客户端登出
	 * 
	 * @param sessionId
	 */
	public static void logout(HttpServletRequest request, HttpServletResponse response, String sessionId) {
		logout(sessionId);
		// 将session从Cookie中删除
		CookieUtil.remove(request, response, GlobalVariable.SSO_SESSIONID);
	}

	/**
	 * 客户端登出
	 * 
	 * @param sessionId
	 */
	public static void logout(String sessionId) {

		String storeKey = SsoSessionIdHelper.parseStoreKey(sessionId);
		if (storeKey == null) {
			return;
		}
		SsoLoginStore.remove(storeKey);
	}

	/**
	 * 客户端登出
	 * 
	 * @param request
	 */
	public static void logout(HttpServletRequest request) {
		String headerSessionId = request.getHeader(GlobalVariable.SSO_SESSIONID);
		logout(headerSessionId);
	}

	/**
	 * 登录校验
	 * 
	 * @param sessionId
	 * @return
	 */
	public static SsoUser loginCheck(String sessionId) {

		// 解析sessionId获取用户Id
		String storeKey = SsoSessionIdHelper.parseStoreKey(sessionId);
		if (storeKey == null) {
			return null;
		}
		// redis获取用户信息
		SsoUser ssoUser = SsoLoginStore.get(storeKey);
		if (ssoUser != null) {
			// 解析sessionId获取版本
			String sessionVersion = SsoSessionIdHelper.parseVersion(sessionId);
			String redisVersion = ssoUser.getVersion();
			log.info("sessionVersion:{} redisVersion:{}",sessionVersion, redisVersion);
			// 版本号相同,说明是同一个用户
			//if (redisVersion.equals(sessionVersion)) {
				// 过期时间超过一半后，自动刷新
				if ((System.currentTimeMillis() - ssoUser.getExpireFreshTime()) > ssoUser.getExpireMinite() / 2) {
					ssoUser.setExpireFreshTime(System.currentTimeMillis());
					SsoLoginStore.put(storeKey, ssoUser);
				}
				return ssoUser;
			//}else {
				//log.info("版本号不相同,说明是不同一个用户");
			//}
		}
		return null;
	}

	/**
	 * 登录校验
	 * 
	 * @param request
	 * @return
	 */
	public static SsoUser loginCheck(HttpServletRequest request) {
		String headerSessionId = request.getHeader(GlobalVariable.SSO_SESSIONID);
		log.info("headerSessionId:{}", headerSessionId);
		if(StrUtil.isBlank(headerSessionId) || StrUtil.equals(headerSessionId, "undefined")) {
			return null;
		}
		return loginCheck(headerSessionId);
	}

}
